﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;

namespace GoldenAgeHelper.common
{
    public class HttpClient : WebClient
    {
        // Cookie 容器 
        private CookieContainer cookieContainer;

        /**/
        /// <summary> 
        /// 创建一个新的 WebClient 实例。 
        /// </summary> 
        public HttpClient()
        {
            this.cookieContainer = new CookieContainer();
            ServicePointManager.Expect100Continue = false;
        }

        /**/
        /// <summary> 
        /// 创建一个新的 WebClient 实例。 
        /// </summary> 
        /// <param name="cookie">Cookie 容器</param> 
        public HttpClient(CookieContainer cookies)
        {
            this.cookieContainer = cookies;
        }

        /**/
        /// <summary> 
        /// Cookie 容器 
        /// </summary> 
        public CookieContainer Cookies
        {
            get { return this.cookieContainer; }
            set { this.cookieContainer = value; }
        }

        /**/
        /// <summary> 
        /// 返回带有 Cookie 的 HttpWebRequest。 
        /// </summary> 
        /// <param name="address"></param> 
        /// <returns></returns> 
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQPinyinSetup 620; QQPinyin 730; CIBA; .NET CLR 2.0.50727; InfoPath.2)";
                //httpRequest.Headers.Add(HttpRequestHeader.Connection,);
                httpRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
                if (httpRequest.Method == "POST")
                {
                    httpRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                    //xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                    httpRequest.Headers.Add("X-Requested-With","XMLHttpRequest");
                    //httpRequest.ContentLength = 60;
                    httpRequest.KeepAlive = true;
                    httpRequest.Headers.Add("Cache-Control", "no-cache");
                    httpRequest.Headers.Add("Accept-Language","zh-cn");
                    httpRequest.Referer = "http://www.nexttime.cn/";
                }
                //httpRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
                httpRequest.CookieContainer = cookieContainer;
                //httpRequest.CookieContainer.GetCookieHeader(new Uri("http://t1.nextworkd.cn"));
            }
            return request;
        }
    }

    //var method = options.method.toUpperCase();
    //    xhr.open(method, url, options.asynchronous);
    //    xhr.onreadystatechange = function(){A.callBack(xhr, options)};
    //    xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
    //    xhr.setRequestHeader('Accept', 'text/javascript, text/html, application/xml, text/xml, */*');
    //    if (method == 'POST')  xhr.setRequestHeader('Content-type', options.contentType + (options.encoding ? '; charset=' + options.encoding : ''));
    //    xhr.send(method == 'POST'? options.postBody : null);

}
